|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
C++ in Plain English
Chapter 2
|
|||||||||||||||||||||||||||
![]() | After entering this program in a source file, you use a compiler or development environment to compile it, which is the process of translating the program into an executable form. I dont discuss specific compilers in this book. For information on how to build a program, see your compiler documentation. |
![]() | Everything in this section applies equally well to the C language, except for one thing: in C, the void keyword in front of main is optional. In C++, it is required. Here, void means that the function (main) does not have a return statement. More about that later. |
You can, of course, print your own string instead of Can you C++ now? Heres the general pattern for the program:
#include <stdio.h>
void main () {
printf(enter-your-string here);
}
Enter any text you want in place of enter-your-string-here.
Programs start to become interesting and useful at the point where they can store and manipulate information. Such programs need a place to put the data: variables.
C++ variable declaration consists of a type name followed by a variable name and a semicolon. The basic (or primitive) types include int, short, long, float, and double, among others. The following example declares two variables of type int.
int variable-name1; int variable-name2;
The C++ variable-declaration syntax is simple. It doesnt involve any extra keyword, such as Dim or var.
You can create multiple data declarations on the same line. Separate each variable with a comma. The following example declares three variables of type short (i, j, k) and three variables of type float (x, y, z).
short i, j, k; float x, y, z;
The basic syntax for data declarations in C++ has another interesting twist. You can initialize variables as they are declared. Doing so gives a variable a starting value but in no way prevents you from changing it later.
To initialize a variable, use the equal sign followed by a value. For example:
int my_var = 0; // my-var initialized to 0 int your_var = 1; // your_var initialized to 1 int a, b = 10, c = 12; // b and c initialized, a is not
Before we proceed, now would be a good time to discuss comments. In C++, a comment consists of all the text starting with the double slashes (//) forward to the end of the tine. A comment is ignored by the compiler. You can put any text in a comment, but people typically use comments to explain a part of their program. When they go back later and look at the source code, the comments help them recall how the program works.
![]() | C++ also supports the begin- and end-comment symbols from the C language (/* and */, respectively). Not all C compilers support the C++ comment-to-end-of-line symbol (//), although many of them do. |
Armed with the ability to declare and initialize data, we can now create a more interesting program:
#include <stdio.h>
void main () {
int x = 1;
int y = 2;
printf(The sum of x + y is %d, x + y);
}
This use of the printf function here requires a little more explanation. The printf function supports formatted output, which means that it can take a numeric argument such as x+y and print the value of this number along with the rest of the string. The format character %d means Print the value of the next argument. Here, d stands for decimal integer. Floating-point values are printed using %f.
All the primitive types are variations on just two kinds of data: integer and floating-point. An integer, as you may have learned in school, is a number that cannot hold fractions (but it can be negative). Floating-point values can have a fractional portion. Floating-point numbers are more flexible, but integers are more efficient. If you know that a certain variable will never need to hold a fractionand there are a great many cases of this, as with a loop counter, for exampledeclare it as an integer.
The basic characteristics of the int, short, long, float, and double data types are summarized in Table 2.1.
| Previous | Table of Contents | Next |
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |